Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-router

Package Overview
Dependencies
Maintainers
2
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-router

Router middleware for koa. Maintained by Forward Email and Lad.

  • 12.0.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is koa-router?

koa-router is a powerful routing middleware for Koa, a next-generation web framework for Node.js. It allows you to define routes for your web application, handle HTTP methods, and manage middleware in a clean and organized manner.

What are koa-router's main functionalities?

Basic Routing

This code demonstrates how to set up a basic route using koa-router. When a GET request is made to the root URL '/', it responds with 'Hello World!'.

const Koa = require('koa');
const Router = require('koa-router');

const app = new Koa();
const router = new Router();

router.get('/', (ctx, next) => {
  ctx.body = 'Hello World!';
});

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000);

Route Parameters

This code demonstrates how to use route parameters with koa-router. When a GET request is made to '/users/:id', it responds with the user ID provided in the URL.

const Koa = require('koa');
const Router = require('koa-router');

const app = new Koa();
const router = new Router();

router.get('/users/:id', (ctx, next) => {
  const userId = ctx.params.id;
  ctx.body = `User ID: ${userId}`;
});

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000);

Middleware

This code demonstrates how to use middleware with koa-router. The logger middleware logs the HTTP method and URL of each request before passing control to the next middleware or route handler.

const Koa = require('koa');
const Router = require('koa-router');

const app = new Koa();
const router = new Router();

const logger = async (ctx, next) => {
  console.log(`${ctx.method} ${ctx.url}`);
  await next();
};

router.get('/', logger, (ctx, next) => {
  ctx.body = 'Hello World!';
});

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000);

Nested Routes

This code demonstrates how to create nested routes with koa-router. The nestedRouter handles requests to '/nested/info' and responds with 'Nested Route Info'.

const Koa = require('koa');
const Router = require('koa-router');

const app = new Koa();
const router = new Router();
const nestedRouter = new Router();

nestedRouter.get('/info', (ctx, next) => {
  ctx.body = 'Nested Route Info';
});

router.use('/nested', nestedRouter.routes(), nestedRouter.allowedMethods());

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000);

Other packages similar to koa-router

Keywords

FAQs

Package last updated on 12 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc